{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建哈希表\n",
    "\n",
    "此代码目的是将相似性矩阵转化为可供快速检索的哈希结构。\n",
    "\n",
    "This code aims to tansfer similarity matrix into hash table for efficient retrieval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.sparse import *\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['common_matrix_from_3238650_to_3598500.npz',\n",
       " 'common_matrix_from_359850_to_719700.npz',\n",
       " 'common_matrix_from_3598500_to_3958350.npz',\n",
       " 'common_matrix_from_3958350_to_4318200.npz',\n",
       " 'common_matrix_from_2878800_to_3238650.npz',\n",
       " 'common_matrix_from_1439400_to_1799250.npz',\n",
       " 'common_matrix_from_2518950_to_2878800.npz',\n",
       " 'common_matrix_from_0_to_359850.npz',\n",
       " 'common_matrix_from_719700_to_1079550.npz',\n",
       " 'common_matrix_from_1079550_to_1439400.npz',\n",
       " 'common_matrix_from_2159100_to_2518950.npz',\n",
       " 'common_matrix_from_1799250_to_2159100.npz']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir('commonMatrix/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finished\n",
      "finished\n",
      "finished\n",
      "finished\n",
      "finished\n",
      "finished\n",
      "finished\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "for name in os.listdir('commonMatrix/'):\n",
    "    mat = load_npz('commonMatrix/' + name).tolil()\n",
    "    l = []\n",
    "    for i in range(mat.shape[0]):\n",
    "        _, a, b = find(mat[i])\n",
    "        index = np.where(b > 1.5)\n",
    "        #l.append(sorted(list(zip(a[index], b[index])),key= lambda x:x[1], reverse=True))\n",
    "        \n",
    "        c = np.array( [round(x,3) for x in b] )\n",
    "        l.append(sorted(list(zip(a[index], c[index])),key= lambda x:x[1], reverse=True))\n",
    "        \n",
    "    l = str(l)\n",
    "    f = open('common_dense_valued_small/' + name, 'w')\n",
    "    f.write(l)\n",
    "    f.close()\n",
    "    print('finished')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#f = open('usersActivity_map.txt', 'r')\n",
    "#m = f.read()\n",
    "#user_times_map = eval(m)\n",
    "#f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "705"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#max( user_times_map.values() )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15244796589352247"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#import math\n",
    "#1/math.log(1+705)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
